package design.prototype;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.*;

/**
 * 序列化实现深拷贝
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Cell3 implements Serializable {
    private String name;
    private Cell3 cell3;
    public Cell3(String name){
        this.name = name;
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Cell3 cell3 = new Cell3("卵细胞", new Cell3("test"));
        //将对象进行序列化输出
        ByteArrayOutputStream byteOut  = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(cell3);
        //把输出的对象进行反序列化
        ByteArrayInputStream byteIn  = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in  = new ObjectInputStream(byteIn);
        Cell3 clone = (Cell3) in.readObject();
        System.out.println(cell3.getCell3().getName());
        System.out.println(clone.getCell3().getName());
        System.out.println(cell3.getCell3() == clone.getCell3());

    }
}
